package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.pojo.DtsTopic;
import com.qf.service.CouponService;
import com.qf.service.TopicService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/topic")
@CrossOrigin
public class TopicController {
    @Autowired
    private TopicService topicService;

    //http://localhost:8083/admin/coupon/list?page=1&limit=20&name=rr&type=1&status=2&sort=add_time&order=desc
    @RequestMapping("/list")
    public Object findCoupon(int page,int limit,String sort,String order,String title,String subtitle){
        IPage<DtsTopic> dtstitle = topicService.selectPage(page, limit, sort, order,title,subtitle);

        Map<String,Object> result =new HashMap<>();

        result.put("total",dtstitle.getTotal());
        result.put("items",dtstitle.getRecords());
        return ResponseUtil.ok(result);
    }

    @PostMapping("/create")
    public Object delete(@RequestBody String body ){
        String title = JacksonUtil.parseString(body, "title");
        String subtitle = JacksonUtil.parseString(body, "subtitle");
        String redCount = JacksonUtil.parseString(body, "redCount");
        Integer price = JacksonUtil.parseInteger(body, "price");
        String goods = JacksonUtil.parseString(body, "goods");
        String picUrl = JacksonUtil.parseString(body, "picUrl");
        String content = JacksonUtil.parseString(body, "content");
        DtsTopic topic=new DtsTopic();
        topic.setTitle(title);
        topic.setSubtitle(subtitle);
        topic.setReadCount(redCount);
        topic.setContent(content);
        topic.setPrice(new BigDecimal(price));
        topic.setPicUrl(picUrl);
        topic.setGoods(goods);
        topicService.creattopic(topic);

        Map<String,Object> result =new HashMap<>();
        result.put("id",topic.getId());
        result.put("title",topic.getTitle());
        result.put("subtitle",topic.getSubtitle());
        result.put("price",topic.getPrice());
        result.put("readCount",topic.getReadCount());
        result.put("goods",topic.getGoods());
        result.put("shareUrl",topic.getShareUrl());
        result.put("content",topic.getContent());
        result.put("picUrl",topic.getPicUrl());
        return ResponseUtil.ok(result);
    }
    @PostMapping("/update")
    public Object updatetopic(@RequestBody String body ){
        String title = JacksonUtil.parseString(body, "title");
        String subtitle = JacksonUtil.parseString(body, "subtitle");
        String redCount = JacksonUtil.parseString(body, "redCount");
        Integer price = JacksonUtil.parseInteger(body, "price");
        String goods = JacksonUtil.parseString(body, "goods");
        String picUrl = JacksonUtil.parseString(body, "picUrl");
        String content = JacksonUtil.parseString(body, "content");
        DtsTopic topic=new DtsTopic();
        topic.setTitle(title);
        topic.setSubtitle(subtitle);
        topic.setReadCount(redCount);
        topic.setContent(content);
        topic.setPrice(new BigDecimal(price));
        topic.setPicUrl(picUrl);
        topic.setGoods(goods);
        topicService.updatetopic(topic);

        Map<String,Object> result =new HashMap<>();
        result.put("id",topic.getId());
        result.put("title",topic.getTitle());
        result.put("subtitle",topic.getSubtitle());
        result.put("price",topic.getPrice());
        result.put("readCount",topic.getReadCount());
        result.put("goods",topic.getGoods());
        result.put("shareUrl",topic.getShareUrl());
        result.put("content",topic.getContent());
        result.put("picUrl",topic.getPicUrl());
        return ResponseUtil.ok(result);
    }
    @PostMapping("/delete")
    public Object creatCoupon(@RequestBody String body ){
        String title = JacksonUtil.parseString(body, "title");
        String subtitle = JacksonUtil.parseString(body, "subtitle");
        String redCount = JacksonUtil.parseString(body, "redCount");
        Integer price = JacksonUtil.parseInteger(body, "price");
        String goods = JacksonUtil.parseString(body, "goods");
        String picUrl = JacksonUtil.parseString(body, "picUrl");
        String content = JacksonUtil.parseString(body, "content");
        DtsTopic topic=new DtsTopic();
        topic.setTitle(title);
        topic.setSubtitle(subtitle);
        topic.setReadCount(redCount);
        topic.setContent(content);
        topic.setPrice(new BigDecimal(price));
        topic.setPicUrl(picUrl);
        topic.setGoods(goods);
        topicService.deletetopic(topic);

        return ResponseUtil.ok();
    }


}
